package org.starsource.forum.client.view;

import java.util.ArrayList;
import java.util.List;

import org.starsource.forum.client.domain.ResourceObject;
import org.starsource.forum.client.widget.ImagePanel;
import org.starsource.forum.client.widget.handler.ImageClickHandler;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.ScrollPanel;

public class ImageSlider extends ScrollPanel {

	private ScrollPanel sPanel = new ScrollPanel();

	private FlowPanel fPanel = new FlowPanel();

	private List<ResourceObject> imageList;

	private List<ImagePanel> imagePanelList = new ArrayList<ImagePanel>();

	private ImageClickHandler imageClickHandler;
	public void setImageClickHandler(ImageClickHandler imageClickHandler) {
		this.imageClickHandler = imageClickHandler;
	}

	public ImageSlider() {
//		this.addStyleName("imageSliderSPanel");
//		hPanel.addStyleName("imageSliderHPanel");
//		hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
//		hPanel.addStyleName("fullScreen");
		sPanel.add(fPanel);
		this.setWidget(sPanel);
	}

	public void setImageList(List<ResourceObject> imageList) {
		this.focus = 0;
		this.imageList = imageList;
		updateImageList();
	}
	public void updateImageList() {
		fPanel.clear();
		imagePanelList.clear();
		for(int index = 0; index < imageList.size(); index ++) {
			ImagePanel imagePanel = new ImagePanel(
					80,80,
					imageList.get(index).getWidth(), imageList.get(index).getHeight(),
					"filedownload?resourceId=" +  imageList.get(index).getId() + "&thumbnail=false",
			"filedownload?resourceId=" +  imageList.get(index).getId() + "&thumbnail=true");
			imagePanel.setImageClickHandler(imageClickHandler);
			imagePanel.setStylePrimaryName("gwter-DecoratorPanel-90");
			imagePanel.addStyleName("displayInline");
			fPanel.add(imagePanel);
			imagePanelList.add(imagePanel);
		}
		sPanel.scrollToBottom();
		if (imageList.size() > 0) {
			this.imageClickHandler.onImageClick(
					"filedownload?resourceId=" +  imageList.get(0).getId() + "&thumbnail=false",
					imageList.get(0).getWidth(), imageList.get(0).getHeight());
		}
	}

	private int focus;
	public void setFocus(int focus) {
		this.focus = focus;
	}

	public void forward() {
		if (focus < imageList.size() - 1) {
			focus ++;
			imageClickHandler.onImageClick("filedownload?resourceId=" +  imageList.get(focus).getId()+"&thumbnail=false",
			imageList.get(focus).getWidth(), imageList.get(focus).getHeight());
			sPanel.setScrollPosition(imagePanelList.get(focus).getElement().getOffsetTop());
			sPanel.ensureVisible(imagePanelList.get(focus));
//			Window.alert(
//			imagePanelList.get(focus).getElement().getOffsetTop() + ":" +
//			imagePanelList.get(focus).getElement().getScrollTop() + ":" +
//			imagePanelList.get(focus).getElement().getAbsoluteTop()
//			);
		}
	}

	public void backward() {
		if (focus > 0) {
			focus --;
			imageClickHandler.onImageClick("filedownload?resourceId=" +  imageList.get(focus).getId()+"&thumbnail=false",
			imageList.get(focus).getWidth(), imageList.get(focus).getHeight());
			sPanel.ensureVisible(imagePanelList.get(focus));
//			Window.alert(
//					imagePanelList.get(focus).getElement().getOffsetTop() + ":" +
//					imagePanelList.get(focus).getElement().getScrollTop() + ":" +
//					imagePanelList.get(focus).getElement().getAbsoluteTop()
//					);
		}
	}

}
